

	

	package Server;

	import java.sql.ResultSet;
	import java.sql.SQLException;
	import java.sql.Statement;

	import client.Login;

	import com.mysql.jdbc.Connection;
	import com.mysql.jdbc.PreparedStatement;
	import java.sql.*;
	import ocsf.server.AbstractServer;
	import ocsf.server.ConnectionToClient;
import java.sql.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
	public class  Compinsert {
		
		static DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
		static Calendar cal = Calendar.getInstance();
		
		/*
		 * on comp array of string comes all the details for the complaint details 
		 * this method add the complaint
		 * 
	     * @author Ariel
	     * @param con1 Connection to the database
	     * @param comp Array of String which old the details of the complaint.comp[2]=User_name,comp[3]=description of the complaint,comp[4]=name of the employ  
     	 * @return  Array of String ,the name of the string is result and on result[1]=1 if we succeed and 0 if we didnt 
	     * 
		 */

		
		public static String[] insertcomp(Connection con1,String[] comp )
		{
			String[] result = new String[2];
			String idc=null;
			 try
	         {
	             Statement stmt = con1.createStatement();
	           
	          
                int RowNum=0;
	            ResultSet res = stmt.executeQuery("SELECT * FROM complaint");
	 			while (res.next()) 
	 				RowNum++;
	             
	 			RowNum++;
	 			//Statement stmt = con1.createStatement();
	 			try {
	 				
				java.sql.PreparedStatement ps = con1.prepareStatement("INSERT INTO complaint VALUES("+RowNum+", '"+comp[2]+"', '"+comp[3]+"', '"+comp[4]+"', 'open', '"+dateFormat.format(cal.getTime())+"')");			
				 ps.executeUpdate();  
				 
	 			} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}	
	             
	             
	             if (res.next()) {
	            	
	  				  				
	     			result[1]="1";

	     		} else     		
	     			result[1]="2";

	     		res.close();
	     		
	     	} catch (SQLException e) {
	     		e.printStackTrace();
	     	}
			
			
			return result;
			
		}
		/*
		 * on comp array of string comes all the details for the complaint details 
		 * this method check if there is a user name under the user name that the employ 
		 * wrote on the gui
		 * if not the gui will show the client the msg and give him another try
		 * 
		 *  
		 */
		

		
		public static String[] chackname(Connection con1,String[] log )
		{
			String[] result = new String[2];
			
			 try
	         {
	             Statement stmt = con1.createStatement();
	           
	          
                int RowNum=0;
	            ResultSet res = stmt.executeQuery("SELECT * FROM clients WHERE User_name='" + log[1] + "'");
	 			while (res.next()) 
	 				RowNum++;
	             
	 			
	 			//Statement stmt = con1.createStatement();
	 			result[1]="1";
                if(RowNum==0){
                	result[1]="0";
                	
                }
                
	     		res.close();
	     		
	     	} catch (SQLException e) {
	     		e.printStackTrace();
	     	}
			
			
			return result;
			
		}
	}	
		